import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_easyrefresh/bezier_hour_glass_header.dart';
import 'package:flutter_easyrefresh/bezier_bounce_footer.dart';
import 'package:wanchong/api/know.dart';
import 'package:wanchong/bean/wend_list_entity.dart';
import '../../path.dart';

class Zsview extends StatefulWidget {
  @override
  _ZsviewState createState() => _ZsviewState();
}

class _ZsviewState extends State<Zsview> {
  GlobalKey<EasyRefreshState> _easyRefreshKey =
      new GlobalKey<EasyRefreshState>();
  GlobalKey<RefreshHeaderState> _headerKey =
      new GlobalKey<RefreshHeaderState>();
  GlobalKey<RefreshFooterState> _footerKey =
      new GlobalKey<RefreshFooterState>();
  int pages = 1;
  List<WendListData> _zsData = List();
  void initState() {
    super.initState();
    if (_zsData.length == 0) {
      myQa(1).listen((WendListEntity data) {
        if (data.code == 200) {
          setState(() {
            this._zsData = data.data;
            this.pages = 2;
          });
        } else {}
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return EasyRefresh(
      key: _easyRefreshKey,
      behavior: ScrollOverBehavior(),
      refreshHeader: BezierHourGlassHeader(
        key: _headerKey,
        color: Color(0xFFFFCC33),
        backgroundColor: Colors.white,
      ),
      refreshFooter: BezierBounceFooter(
        key: _footerKey,
        color: Color(0xFFFFCC33),
        backgroundColor: Colors.white,
      ),
      child: _listZs(),
      onRefresh: () async {
        await new Future.delayed(const Duration(seconds: 1), () {
          setState(() {
            myQa(1).listen((WendListEntity data) {
              if (data.code == 200) {
                setState(() {
                  this._zsData.clear();
                  this._zsData = data.data;
                  this.pages = 2;
                });
              } else {}
            });
          });
        });
      },
      loadMore: () async {
        await new Future.delayed(const Duration(seconds: 1), () {
          myQa(1).listen((WendListEntity data) {
            if (data.code == 200) {
              setState(() {
                this._zsData.addAll(data.data);
                this.pages++;
              });
            } else {}
          });
        });
      },
    );
  }

  Widget _listZs() {
    List<Widget> tiles = [];
    Widget ContainerGl;
    for (var item in _zsData) {
      tiles.add(new GestureDetector(
        onTap: () {
          Navigator.of(context).pushNamed(WebviewzPath);
        },
        child: Container(
          margin: EdgeInsets.only(left: 15, right: 15, top: 15),
          height: 110,
          decoration: BoxDecoration(
              border: Border.all(color: Colors.white, width: 1.0),
              color: Colors.white,
              borderRadius: BorderRadius.circular(5.0)),
          child: Column(
            children: <Widget>[
              Row(
                children: <Widget>[
                  Container(
                      width: 20,
                      height: 20,
                      margin: EdgeInsets.only(left: 15),
                      child: Image.asset('assets/user/zhishi_question_icon.png',
                          width: 20, height: 20, fit: BoxFit.cover)),
                  Expanded(
                    child: Container(
                      height: 50,
                      padding: EdgeInsets.only(right: 15.0),
                      margin: EdgeInsets.only(left: 5, top: 15),
                      child: Text(
                        item.title,
                        textAlign: TextAlign.left,
                        maxLines: 2,
                        overflow: TextOverflow.ellipsis,
                        style: new TextStyle(
                            fontSize: 18.0, color: Color(0xFF3C4450)),
                      ),
                    ),
                  ),
                ],
              ),
              Row(
                children: <Widget>[
                  Container(
                    margin: EdgeInsets.only(left: 15, top: 15),
                    child: Text(
                      '20',
                      textAlign: TextAlign.left,
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style: new TextStyle(
                          fontSize: 12.0, color: Color(0xFFB4BEC6)),
                    ),
                  ),
                  Expanded(
                    child: Container(
                      margin: EdgeInsets.only(left: 5, top: 14),
                      padding: EdgeInsets.only(right: 15.0),
                      child: Text(
                        '赞同',
                        textAlign: TextAlign.left,
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: new TextStyle(
                            fontSize: 12.0, color: Color(0xFFB4BEC6)),
                      ),
                    ),
                  ),
                ],
              )
            ],
          ),
        ),
      ));
    }
    ContainerGl = new ListView(
      children: tiles,
    );
    return ContainerGl;
  }
}
